﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.OleDb;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminChipTags : Form
    {
        private int groupId;

        private string file = string.Empty;
        private DataTable dtImportData = new DataTable();
        
        private OleDbConnection oleConn;
        private OleDbDataAdapter oleAdapter;

        // Constructor
        public frmAdminChipTags(int groupId, string groupName)
        {
            InitializeComponent();
            this.groupId = groupId;
            this.Icon = Properties.Resources.AtletIS64;
            this.lblTitle.Text = groupName;
        }

        // Form Load
        private void frmAdminChipTags_Load(object sender, EventArgs e)
        {
            DatabaseHelper.FillChipTagsByChipTagsGroupId(this.aDbDataSet.ChipTags, this.groupId);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.bsChipTags.EndEdit();

                foreach (DataRow dr in this.aDbDataSet.ChipTags.Rows)
                {
                    if (dr.RowState != DataRowState.Deleted)
                    {
                        if (dr["ChipTagsGroupId"] == DBNull.Value || Convert.ToInt32(dr["ChipTagsGroupId"]) != this.groupId)
                            dr["ChipTagsGroupId"] = this.groupId;
                    }

                    if (dr.RowState == DataRowState.Added)
                        dr["Sync"] = false;
                }

                this.taChipTags.Update(this.aDbDataSet.ChipTags);

                MessageBox.Show("Zmeny boli uložené!", "Chip Tags", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Chip Tags", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Delete all
        private void tsbDeleteAll_Click(object sender, EventArgs e)
        {
            try
            {
                aDbDataSet.ChipTagsDataTable dtChipTags = new aDbDataSet.ChipTagsDataTable();
                DatabaseHelper.FillChipTagsByChipTagsGroupId(dtChipTags, this.groupId);
                if (dtChipTags != null && dtChipTags.Rows.Count > 0)
                {
                    for (int i = dtChipTags.Rows.Count - 1; i >= 0; i--)
                    {
                        dtChipTags.Rows[i].Delete();
                    }
                }

                // Update
                taChipTags.Update(dtChipTags);

                // Reload grid
                DatabaseHelper.FillChipTagsByChipTagsGroupId(this.aDbDataSet.ChipTags, this.groupId);

                MessageBox.Show("Všetky záznamy boli zmazané!", "Import", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Zmazať všetko", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Import
        private void tsmiExcelImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Title = "Vyberte súbor pre import";
            openDialog.Filter = "Súbory programu Excel|*.xls;*.xlsx";

            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                file = openDialog.FileName;

                try
                {
                    oleConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" + file + "'; Extended Properties=Excel 8.0;");
                    oleConn.Open();

                    DataTable dtSchema = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, null });

                    dtImportData.Rows.Clear();
                    oleAdapter = new OleDbDataAdapter(string.Format("SELECT CHIP_TAG, ST_NUM FROM [{0}]", dtSchema.Rows[0]["TABLE_NAME"].ToString()), oleConn);
                    oleAdapter.Fill(dtImportData);

                    //aDbDataSet.ChipTagsDataTable dtChipTags = taChipTags.GetData();


                    aDbDataSet.ChipTagsDataTable dtChipTags = new aDbDataSet.ChipTagsDataTable();
                    foreach (DataRow drImport in dtImportData.Rows)
                    {
                        dtChipTags.Rows.Add(this.groupId, drImport["CHIP_TAG"].ToString(), drImport["ST_NUM"], false, DateTime.Now);
                    }

                    // Update
                    taChipTags.Update(dtChipTags);

                    // Reload grid
                    DatabaseHelper.FillChipTagsByChipTagsGroupId(this.aDbDataSet.ChipTags, this.groupId);

                    MessageBox.Show("Import prebehol úspešne!", "Import", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    oleConn.Close();
                    oleConn.Dispose();
                }
            }
        }

        // Export
        private void tsmiExcelExport_Click(object sender, EventArgs e)
        {
            ChipTagsExportHelper.Export(this.groupId);
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
